package com.yhy.wxzn.wcs.websocket.bean;


import cn.hutool.core.lang.UUID;
import com.yhy.wxzn.common.utils.SpringUtil;
import com.yhy.wxzn.wcs.SystemInit;
import com.yhy.wxzn.wcs.device.rgv.bean.Coord;
import com.yhy.wxzn.wcs.device.rgv.bean.FourWayRgv;
import com.yhy.wxzn.wcs.device.rgv.enums.PalletStatus;
import com.yhy.wxzn.wcs.device.rgv.enums.RgvStatus;
import com.yhy.wxzn.wcs.entity.WcsMapInfo;
import com.yhy.wxzn.wcs.service.impl.WcsMapInfoServiceImpl;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.util.Objects;

@Data
public class NodeInfo {

    private CoordinationDetailedInfo coordinationDetailedInfo;

    private RgvInfo rgvInfo;



    public static NodeInfo buildNodInfo(String nodeId){
        WcsMapInfo info = SpringUtil.getBean(WcsMapInfoServiceImpl.class).getWcsMapInfoById(nodeId);
        NodeInfo nodeInfo = new NodeInfo();
        nodeInfo.setCoordinationDetailedInfo(CoordinationDetailedInfo.buildCoordinationDetailedInfo(info));
        try {
            nodeInfo.setRgvInfo(RgvInfo.buildRgvInfo(info));

        }catch (Exception e){}

        return nodeInfo;
    }



    @Data
    static class TestData1{
        String t1;
        String t2;
        String t3;
        String t4;
        String t5;
        public static TestData1 buildTestData1(){
            TestData1 testData1 = new TestData1();
            testData1.setT1(UUID.fastUUID().toString());
            testData1.setT2(UUID.fastUUID().toString());
            testData1.setT3(UUID.fastUUID().toString());
            testData1.setT4(UUID.fastUUID().toString());
            testData1.setT5(UUID.fastUUID().toString());
            return testData1;
        }
    }

    @Data
    static class TestData2{
        String t1;
        String t2;
        String t3;
        String t4;
        String t5;
        public static TestData2 buildTestData2(){
            TestData2 testData1 = new TestData2();
            testData1.setT1(UUID.fastUUID().toString());
            testData1.setT2(UUID.fastUUID().toString());
            testData1.setT3(UUID.fastUUID().toString());
            testData1.setT4(UUID.fastUUID().toString());
            testData1.setT5(UUID.fastUUID().toString());
            return testData1;
        }
    }

    @Data
    static class TestData3{
        String t1;
        String t2;
        String t3;
        String t4;
        String t5;

        public static TestData3 buildTestData3(){
            TestData3 testData1 = new TestData3();
            testData1.setT1(UUID.fastUUID().toString());
            testData1.setT2(UUID.fastUUID().toString());
            testData1.setT3(UUID.fastUUID().toString());
            testData1.setT4(UUID.fastUUID().toString());
            testData1.setT5(UUID.fastUUID().toString());
            return testData1;
        }
    }

    @Data
    static class RgvInfo{
        //当前位置
        String curLocation;
        //托盘状态
        PalletStatus palletStatus;
        //小车状态
        RgvStatus rgvStatus;
        //错误信息
        String errorMsg;
        //地码
        String groundCode;
        //X偏移量
        Integer xvar;
        //Y偏移量
        Integer yvar;
        //任务Id
        Integer taskId;

        Integer rgvId;


        public static RgvInfo buildTestRgvInfo(WcsMapInfo wcsMapInfo){
            RgvInfo rgvInfo = null;

            for (FloorInfo.RgvInfo info : FloorInfo.RgvInfo.RgvDriveInfo.rgvInfos) {
                if (Objects.equals(info.getLocation(),new Coord(wcsMapInfo.getX(),wcsMapInfo.getY(),wcsMapInfo.getZ()))){
                    rgvInfo = new RgvInfo();
                    rgvInfo.setCurLocation(info.getLocation().toString());
                    rgvInfo.setPalletStatus(PalletStatus.UNKNOWN);
                    rgvInfo.setRgvStatus(RgvStatus.RUNNING);
                    rgvInfo.setErrorMsg("");
                    rgvInfo.setGroundCode("11111");
                    rgvInfo.setXvar(1);
                    rgvInfo.setYvar(1);
                    rgvInfo.setTaskId(1);
                    rgvInfo.setRgvId(info.getRgvNo());
                    break;
                }
            }

            return rgvInfo;
        }

        public static RgvInfo buildRgvInfo(WcsMapInfo wcsMapInfo){
            RgvInfo rgvInfo = null;
            for (FourWayRgv rgv : SystemInit.fourWayRgvs) {
                if (Objects.equals(rgv.getFourCarPlc().getTwoCode(),wcsMapInfo.getGroundCode())){
                     rgvInfo = new RgvInfo();
                    rgvInfo.setCurLocation(rgv.getCurLocation().toString());
                    rgvInfo.setPalletStatus(rgv.getFourCarPlc().getPalletStatus());
                    rgvInfo.setRgvStatus(rgv.getRgvStatus());
                    rgvInfo.setErrorMsg(rgv.getFourCarPlc().getErrorMessage());
                    rgvInfo.setGroundCode(rgv.getFourCarPlc().getTwoCode());
                    rgvInfo.setXvar(rgv.getFourCarPlc().getXvar());
                    rgvInfo.setYvar(rgv.getFourCarPlc().getYvar());
                    rgvInfo.setTaskId(rgv.getCurTask() == null ? null: rgv.getCurTask().getTaskId());
                    rgvInfo.setRgvId(rgv.getDeviceInfo().getId());
                    break;
                }
            }
            return rgvInfo;
        }
    }
    @Data
    static class CoordinationDetailedInfo{
        String nodeId;
        Integer x;
        Integer y;
        Integer z;
        //节点类型
        Integer nodeType;
        //托盘码
        String palletCode;
        //地码
        String groundCode;
        //有无货
        String nodeStatus;


        public static CoordinationDetailedInfo buildCoordinationDetailedInfo(WcsMapInfo wcsMapInfo){
            CoordinationDetailedInfo coordinationDetailedInfo = new CoordinationDetailedInfo();
            BeanUtils.copyProperties(wcsMapInfo,coordinationDetailedInfo);
            coordinationDetailedInfo.setNodeId(wcsMapInfo.getId());

            return coordinationDetailedInfo;
        }
    }
}
